package com.nononsenseapps.feeder.db.entity

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.Index
import androidx.room.PrimaryKey
import com.nononsenseapps.feeder.db.*
import com.nononsenseapps.feeder.db.room.ID_UNSET
import com.nononsenseapps.feeder.util.sloppyLinkToStrictURL
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import java.net.URL

const val MAX_ATTENTION_LEVEL=4
const val ATTENTION_NORMAL=2

const val ITEM_TYPE_NO_SET=0
const val ITEM_TYPE_POSTHASTE=1
const val ITEM_TYPE_AUDIO=10
const val ITEM_TYPE_VIDEO=10

@Entity(tableName = FEED.tableName,
        indices = [Index(value = [FEED.COL_URL], unique = true),
            Index(value = [FEED.COL_ID, FEED.COL_URL, FEED.COL_TITLE], unique = true)])
data class Feed @Ignore constructor(
    @PrimaryKey(autoGenerate = true)
        @ColumnInfo(name = FEED.COL_ID) var id: Long = ID_UNSET,
    @ColumnInfo(name = FEED.COL_TITLE) var title: String = "",
    @ColumnInfo(name = FEED.COL_CUSTOM_TITLE) var customTitle: String = "",
    @ColumnInfo(name = FEED.COL_URL) var url: URL = sloppyLinkToStrictURL(""),
    @ColumnInfo(name = FEED.COL_TAG) var tag: String = "",
    @ColumnInfo(name = FEED.COL_NOTIFY) var notify: Boolean = false,
    @ColumnInfo(name = FEED.COL_IMAGE_URL) var imageUrl: URL? = null,
    @ColumnInfo(name = FEED.COL_LAST_SYNC_TIME, typeAffinity = ColumnInfo.INTEGER) var lastSync: DateTime = DateTime(0L, DateTimeZone.UTC),
    @ColumnInfo(name = FEED.COL_LAST_SUCCESS_SYNC_TIME, typeAffinity = ColumnInfo.INTEGER) var lastSuccessSyncTime: DateTime = DateTime(0L, DateTimeZone.UTC),
    @ColumnInfo(name = FEED.COL_RESPONSE_HASH) var responseHash: Int = 0,
    @ColumnInfo(name = FEED.COL_FEED_SET_OPEN_TYPE) var openType: Int = 0,
    @ColumnInfo(name = FEED.COL_FEED_SET_OPEN_TYPE_TREE_NAME, defaultValue = "") var openTypeTreeName: String = "",
    @ColumnInfo(name = FEED.COL_FEED_STATE_NET_ERR) var netErrCount: Int = 0,
    @ColumnInfo(name = FEED.COL_FEED_LAST_SYNC_SUCCESS) var syncSuccess: Boolean = true,
    @ColumnInfo(name = FEED.COL_FEED_SET_SYNC_ABLE) var syncAble: Boolean = true,
    @ColumnInfo(name = FEED.COL_FEED_SET_SYNC_PERIOD) var sync_period: Int = 0,
    @ColumnInfo(name = FEED.COL_FEED_SYNC_NEW_ITEM_COUNT) var syncNewItemCount: Int = 0,
    @ColumnInfo(name = FEED.COL_ATTENTION_LEVEL) var attentionLevel: Int = ATTENTION_NORMAL,
    @ColumnInfo(name = FEED.COL_RULE_ABLE) var ruleAble: Boolean = false,
    @ColumnInfo(name = FEED.COL_BACKUP) var backup: Boolean = false,
    @ColumnInfo(name = FEED.COL_OVERWRITE) var overWrite: Boolean = false,

    @ColumnInfo(name = FEED.COL_FEED_ITEM_TYPE) var itemType: Int = ITEM_TYPE_NO_SET,
    @ColumnInfo(name = FEED.COL_FULL_TEXT_SYNC, defaultValue = "0") var fullTextSync: Boolean = false,
    @ColumnInfo(name = FEED.COL_PRE_DOWNLOAD, defaultValue = "0") var preDownload: Boolean = false,
    @ColumnInfo(name = FEED.COL_IMAGE_PRE_DOWNLOAD, defaultValue = "0") var imagePreDownload: Boolean = false,
    @ColumnInfo(name = FEED.COL_FEED_ITEM_BODY_FORMAT_TYPE) var itemBodyFormatType: Int = ITEM_BODY_FORMAT_TYPE_NO_SET,

    @ColumnInfo(name = FEED.COL_LINK_CONF, defaultValue = "") var link_conf: String = "",
    @ColumnInfo(name = FEED.COL_XPATH_CONF, defaultValue = "") var xpath_conf: String = "",

    @ColumnInfo(name = FEED.COL_FEED_ITEM_BODY_REFORMAT, defaultValue = "0") var bodyReFormat: Boolean = false,
    @ColumnInfo(name = FEED.COL_MUTABLE_SERVER_FEED, defaultValue = "0") var mutableServerFeed: Boolean = false,
    @ColumnInfo(name = FEED.COL_FEED_ITEM_READ_LIST_ALL, defaultValue = "0") var readListAll: Boolean = false
) {
    constructor() : this(id = ID_UNSET)

    val displayTitle: String
        get() = (if (customTitle.isBlank()) title else customTitle)

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Feed

        if (id != other.id) return false

        return true
    }

    override fun hashCode(): Int {
        return id.hashCode()
    }


}

